package com.atguigu.flink.wordcount;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import static org.apache.flink.api.common.typeinfo.Types.*;


/*
        使用本地的WEBUI界面查看FLink程序
 */
public class Demo8_LocalWEBUI
{
    public static void main(String[] args) throws Exception {

        //设置web应用绑定的主机名(windows本地)和端口号
        //存放配置信息
        Configuration conf = new Configuration();
        conf.setInteger("rest.port",3333);

        //读取配置，创建一个提供本地WEBUI监控的环境。 启动程序后，可以访问  localhost:3333查看程序
        StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(conf);

        //env.setParallelism(1);
        DataStreamSource<String> ds = env.socketTextStream("hadoop103", 8888);

        ds.flatMap((FlatMapFunction<String, Tuple2<String, Integer>>) (value, out) -> {
            String[] words = value.split(" ");
            for (String word : words) {
                out.collect(Tuple2.of(word,1));
            }
        }).setParallelism(4)
          .returns(TUPLE(STRING,INT))
          .keyBy( t ->   t.f0)
          .sum(1).setParallelism(2)
          .print().setParallelism(2);


        env.execute();

    }
}
